//
//
//

#include <iostream>
#include <irrlicht.h>
#include <time.h>
#include "CActionServer.h"


#pragma comment (lib, "ws2_32.lib")
#pragma comment (lib, "../irrNetLite/lib/irrNetLite.lib")

CActionServer*	CActionServer::m_pInstance = NULL;


using	namespace	irr;
using	namespace	core;


CActionServer*	CActionServer::Instance()
{
	if ( m_pInstance == NULL )
	{
		m_pInstance = new CActionServer;
		if ( !m_pInstance->Init() )
			m_pInstance = NULL;
	}
	return m_pInstance;
}


bool	CActionServer::Init()
{
	CLuaLoader* lua = CLuaLoader::Instance();

	m_pNetwork = irr::net::createIrrNetServer(0);

	m_pNetwork->setNetCallback( this );
	//m_pNetwork->setVerbose(true);

	std::cout << "Initializing finished!\n";

	return true;
}


bool	CActionServer::Run()
{
	while ( m_pNetwork->getConnectionStatus() != irr::net::EICS_FAILED )
	{
		m_pNetwork->update(16);
		SendbackInfo();
		//std::cout << "server on\n";
	}

	return false;
}


void	CActionServer::handlePacket(irr::net::SInPacket& packet)
{
	vector3df v3;
	packet >> v3;
	std::cout << v3.X << " " << v3.Y << " " << v3.Z << std::endl;
	//m_pNetwork
// 
// 	c8 game_status;
// 	packet >> game_status;
// 	if ( game_status == 0 )
// 	{
// 		//start game
// 		u32 table_id;
// 		u32 avatar_num;
// 		u32 seat_id;
// 		irr::core::stringc avatar_name;
// 		packet >> table_id;
// 		packet >> avatar_num;
// 		packet >> seat_id;
// 		packet >> avatar_name;
// 
// 		bool	flag = false;
// 		for ( int i = 0; i < m_vGameInstances.size(); i++ )
// 		{
// 			if ( m_vGameInstances[i].m_iTableID == table_id )
// 			{
// 				flag = true;
// 			}
// 		}
// 		if ( !flag )
// 		{
// 			//new game
// 			CGameInstance instance;
// 			instance.m_iTableID = table_id;
// 
// 			for ( int m = 0; m < avatar_num; m++ )
// 			{
// 				
// 				instance.m_vAvatars.clear();
// 				CServerAvatar avatar;
// 				//avatar.m_iSeatID = 0;
// 				avatar.m_iSeatID = m + 1;
// 				instance.m_vAvatars.push_back( avatar );
// 
// 			}
// 			m_vGameInstances.push_back( instance );
// 		}
// 		
// 		//create avatar info
// 		int m;
// 		for ( m = 0; m < m_vGameInstances.size(); m++ )
// 		{
// 			if ( m_vGameInstances[m].m_iTableID == table_id )
// 			{
// 				break;
// 			}
// 		}
// 		int n = seat_id - 1;
// 		m_vGameInstances[m].m_vAvatars[n].m_bInGame = true;
// 		m_vGameInstances[m].m_vAvatars[n].m_sName = avatar_name;
// 	}
// 	else
// 	{
// 		//in game
// 		u32 instance_id;
// 		u32 avatar_id;
// 		vector3df	position;
// 		irr::core::stringc status_type;
// 		u32 status_statue;
// 		s32 time;
// 
// 		packet >> instance_id;
// 		packet >> avatar_id;
// 		packet >> position;
// 		packet >> status_type;
// 		packet >> status_statue;
// 		packet >> time;
// 
// 		if ( time > this->m_vGameInstances[ instance_id ].m_iTime )
// 		{
// 			m_vGameInstances[instance_id].m_vAvatars[avatar_id].m_iTime = time;
// 			m_vGameInstances[instance_id].m_vAvatars[avatar_id].m_v3Position = position;
// 			//m_vGameInstances[instance_id].
// 		}
// 
// 	}
}

bool	CActionServer::SendbackInfo()
{
	irr::net::SOutPacket packet;

	for ( int i = 0; i < m_vGameInstances.size(); i++ )
	{
		//sending back info for every instance
		m_vGameInstances[i].m_iTime = time(0);

		packet.clearData();
		if ( m_vGameInstances[i].m_bInGame )
		{
			packet << (u32)1;
			packet << (u32)i;
			for ( int j = 0; j < m_vGameInstances[i].m_vAvatars.size(); j++ )
			{
				packet << (u32)j;
				packet << m_vGameInstances[i].m_vAvatars[j].m_v3Position;
				packet << m_vGameInstances[i].m_vAvatars[j].m_status.action_name;
				packet << m_vGameInstances[i].m_vAvatars[j].m_status.status;
			}
		}
		
	}

	return	true;
}
